package com.yl.chainStore.payrule.item

import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.entity.pay.PayCustomerOrderDetail
import com.yl.chainStore.payrule.common.DiscountComputeOrderResultVo
import com.yl.common.control.ResponseBody

abstract class ItemRuleService {

	/**
	 * 项目优惠
	 */
	abstract fun computeOrder(order: PayCustomerOrder): DiscountComputeOrderResultVo


	/**
	 * 部分项目折扣某些金额处理
	 * 只处理订单明细
	 * 把实际折扣的金额返回（可能折扣的金额大于订单价格）
	 */
	fun discountPartDetail(details:Collection<PayCustomerOrderDetail>,discountMoney:Long):Long{
		var originalPrice = 0L               //应付金额
		//算出实际总额
		for (detail in details) {
			detail.realMoney = detail.realPrice * detail.number
			originalPrice += detail.realMoney
		}
		val realDiscountMoney = if(discountMoney < originalPrice){
			discountMoney
		}else{
			originalPrice
		}
		val realPayMoney = originalPrice - realDiscountMoney  //实际付款


		//处理实际价格
		for (detail in details) {
			if(originalPrice == 0L){
				detail.realPrice = 0
				detail.realMoney = 0
			}else{
				detail.realPrice = detail.realPrice.times(realPayMoney).div(originalPrice)
				detail.realMoney = detail.realPrice.times(detail.number)
			}
		}
		return realDiscountMoney

	}

}
